//
//  OCRViewController.m
//  QuanNengSaoMiaoZhuShou
//
//  Created by 于博文 on 16/3/12.
//  Copyright © 2016年 于博文. All rights reserved.
//

#import "OCRViewController.h"
#import "ProgressView.h"

@interface OCRViewController ()<G8TesseractDelegate>
@property (nonatomic,strong)UIActivityIndicatorView *activityIndicator;
@property (nonatomic,strong)NSOperationQueue *operationQueue;
@property (nonatomic,strong)ProgressView *progressV;
@property (nonatomic,strong)UIView *shadeView;
@property (nonatomic,strong)UIButton *button;
///存储需要解析的图片
@property (nonatomic,strong)UIImage *recognizeImage;

@end

@implementation OCRViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",self.arrayForImage);
    //
    self.OCRImageV = [[UIImageView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.origin.x + 20, [UIScreen mainScreen].bounds.origin.y + 20, [UIScreen mainScreen].bounds.size.width - 40, [UIScreen mainScreen].bounds.size.height -80)];
    //解析按钮
    self.button = [[UIButton alloc]initWithFrame:CGRectMake(self.view.center.x - 25, self.view.frame.size.height*.8, 50, 30)];
    [self.button addTarget:self action:@selector(recognizeAction) forControlEvents:(UIControlEventTouchUpInside)];
    //队列
    self.operationQueue = [[NSOperationQueue alloc]init];
}
//解析按钮
- (void)recognizeAction{
    [self recognizeImageWithTesseract:self.recognizeImage];
}
//解析方法
- (void)recognizeImageWithTesseract:(UIImage *)image{
    UIImage *showImage = [image g8_blackAndWhite];
    //设置菊花
    [self setProgressView];
    //这里设置要展示出来的图片
    self.OCRImageV.image = showImage;
    //1,设置语言
    G8RecognitionOperation *operation = [[G8RecognitionOperation alloc]initWithLanguage:@"chi_sim"];
    //2,设置解析模式
    operation.tesseract.engineMode = G8OCREngineModeTesseractOnly;
    operation.tesseract.pageSegmentationMode = G8PageSegmentationModeAutoOnly;
    
    //3,设置代理
    operation.delegate = self;
    
    //4,设置解析源
    operation.tesseract.image = showImage;
    
    //5,完成识别
    operation.recognitionCompleteBlock = ^(G8Tesseract *resseract){
        //获取识别出来的文字
        NSString *recingnizedText = resseract.recognizedText;
        NSLog(@"%@",recingnizedText);
        //此处为主线程
        //此处移除菊花
        self.shadeView.hidden = YES;
    };
    
    [self.operationQueue addOperation:operation];
}
//观察进度的代理
- (void)progressImageRecognitionForTesseract:(G8Tesseract *)tesseract{
    //注意: 此处为子线程
    NSLog(@"已经解析 : %lu",tesseract.progress);
    
    //CGFloat temp = (CGFloat)(tesseract.progress-30)/56.6;
    
    //NSLog(@"%f",(CGFloat)(tesseract.progress));
    
    dispatch_async(dispatch_get_main_queue(), ^{
        CGFloat value =(CGFloat)(tesseract.progress-30)/56.6;
        
        if (value > 1) {
            value = 0.99;
            self.progressV.progressValue = value;
        }else{
            
            self.progressV.progressValue = value;
        }
    });


}

- (void)setProgressView{
    ProgressView *circleProgress2 = [ProgressView viewWithFrame:CGRectMake(80, 200, 100, 100)
                                                    circlesSize:CGRectMake(34, 2, 30, 30)];
    circleProgress2.layer.cornerRadius = 10;
    
    //阴影
    circleProgress2.backgroundColor = [UIColor clearColor];
    circleProgress2.backCircle.shadowColor = [UIColor grayColor].CGColor;
    circleProgress2.backCircle.shadowRadius = 3;
    circleProgress2.backCircle.shadowOffset = CGSizeMake(0, 0);
    circleProgress2.backCircle.shadowOpacity = 1;
    circleProgress2.backCircle.fillColor = [UIColor colorWithRed:151/255.0 green:151/255.0 blue:151/255.0 alpha:0.8].CGColor;
    circleProgress2.backCircle.strokeColor = [UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1].CGColor;
    
    circleProgress2.foreCircle.lineCap = @"butt";
    circleProgress2.foreCircle.strokeColor = [UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1].CGColor;;
    circleProgress2.progressValue = 0;
    
    [self.shadeView addSubview:circleProgress2];
    self.progressV = circleProgress2;
}

//- (void)setProgressView{
//    ProgressView *circleProgress2 = [ProgressView viewWithFrame:CGRectMake(80, 200, 100, 100)
//                                                    circlesSize:CGRectMake(34, 2, 30, 30)];
//    circleProgress2.layer.cornerRadius = 10;
//    
//    //阴影
//    circleProgress2.backgroundColor = [UIColor clearColor];
//    circleProgress2.backCircle.shadowColor = [UIColor grayColor].CGColor;
//    circleProgress2.backCircle.shadowRadius = 3;
//    circleProgress2.backCircle.shadowOffset = CGSizeMake(0, 0);
//    circleProgress2.backCircle.shadowOpacity = 1;
//    circleProgress2.backCircle.fillColor = [UIColor colorWithRed:151/255.0 green:151/255.0 blue:151/255.0 alpha:0.8].CGColor;
//    circleProgress2.backCircle.strokeColor = [UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1].CGColor;
//    
//    circleProgress2.foreCircle.lineCap = @"butt";
//    circleProgress2.foreCircle.strokeColor = [UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1].CGColor;;
//    circleProgress2.progressValue = 0;
//    
//    [self.shadeView addSubview:circleProgress2];
//    self.progressV = circleProgress2;
//}

#pragma mark - 懒加载
- (NSMutableArray *)arrayForImage{
    if (!_arrayForImage) {
        _arrayForImage = [NSMutableArray array];
    }
    return _arrayForImage;
}
- (UIView *)shadeView{
    if (!_shadeView) {
        _shadeView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        _shadeView.alpha = 0.3;
        _shadeView.backgroundColor = [UIColor blackColor];
        [self.view addSubview:_shadeView];
    }
    return _shadeView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
